package com.ljz.servlet;

import com.ljz.entity.User;
import com.ljz.service.UserService;
import com.ljz.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        User user = new User();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        user.setUname(username);
        user.setPassword(password);
        int num = userService.addUser(user);

        if (num>0){
            out.print("<script>alert('注册成功，请登录')</script>");
            out.print("<script>location.href='login.html'</script>");
        } else {
            out.print("<script>alert('注册失败，请重新注册')</script>");
            out.print("<script>location.href='register.html'</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
